package com.codeoss.framework.view.pagebean.mobile;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.clientscript.NavigatePageClientScript;
import com.codeoss.framework.view.component.DataGrid;
import com.codeoss.framework.view.component.FilterGroup;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.mobile.MListView;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.pagebean.base.BasePageBean;
import com.codeoss.framework.view.pagebean.crud.PageBeanCRUDUtil;
import com.codeoss.framework.view.pagebean.crud.SelectRequest;
import com.codeoss.framework.view.pagebean.crud.SelectResponse;
import com.codeoss.framework.view.service.IFilterGroupComponentService;
import com.codeoss.framework.view.util.DataSourceUtil;
import com.codeoss.framework.view.util.PageBeanUtil;

@Service("mListViewPageBean")
public class MListViewPageBean extends BasePageBean {
	public static final String KEY_MLISTVIEW = "key_mlistview";

	public static final String KEY_MLISTVIEW_FIELD_LIST = "key_mlistview_field_list";

	public static final String KEY_DATAGRID = "key_datagrid";

	public static final String KEY_DATAGRID_FIELD_LIST = "key_datagrid_field_list";

	public static final String KEY_FILTERGROUP = "key_filtergroup";

	@Autowired
	private IFilterGroupComponentService filterGroupComponentService;
	@Autowired
	private PageBeanUtil pageBeanUtil;
	@Autowired
	private DataSourceUtil dataSourceUtil;
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private PageBeanCRUDUtil pageBeanCRUDUtil;

	public void beforeInit() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		if(!page.isInit()) {
			pageBeanUtil.getComponent(yunContext, page, "mlistview");
			MListView mListView = (MListView) yunContext.getAttribute("__component__");
			page.setAttribute(KEY_MLISTVIEW, mListView);

			List<IDataBinding> fieldList = new ArrayList<IDataBinding>();
			pageBeanUtil.getDataBindingList(mListView, fieldList);
			page.setAttribute(KEY_MLISTVIEW_FIELD_LIST, fieldList);

			pageBeanUtil.getComponent(yunContext, page, "datagrid");
			DataGrid dataGrid = (DataGrid) yunContext.getAttribute("__component__");
			page.setAttribute(KEY_DATAGRID, dataGrid);
			
			pageBeanUtil.getComponent(yunContext, page, "filtergroup");
			FilterGroup filterGroup = (FilterGroup) yunContext.getAttribute("__component__");
			page.setAttribute(KEY_FILTERGROUP, filterGroup);
			
			page.setInit(true);
		}
	}
	
	public void init() {
		dataSearch();
	}

	public void dataSearch() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		DataSourceConfig dataSourceConfig = page.getDataSourceConfigList().get(0);
		Table table = tableStore.get(dataSourceConfig.getTableName());
		yunContext.setMasterModel(dataSourceConfig.getTableName());
		Query query = new Query();

		String componentId = "";
		MListView mListView = (MListView) page.getAttribute(KEY_MLISTVIEW);
		DataGrid dataGrid = (DataGrid) page.getAttribute(KEY_DATAGRID);
		if (mListView != null) {
			componentId = mListView.getId();
		} else {
			componentId = dataGrid.getId();
		}

		if (yunContext.isPostBack()) {
			String nextPage = yunContext.getViewRequest().getData().get(componentId + "_page");
			if (!StringUtils.isEmpty(nextPage)) {
				query.setPage(Integer.parseInt(nextPage));
			}
		} else {
			query.setPage(1);
		}

		if (mListView != null) {
			query.setLimit(mListView.getPageSize());
			List<IDataBinding> fieldList = page.getAttribute(KEY_MLISTVIEW_FIELD_LIST);
			dataSourceUtil.fillQuery(yunContext, query, dataSourceConfig);

			SelectRequest selectRequest = new SelectRequest();
			selectRequest.setMainTable(table);
			selectRequest.setMainQuery(query);

			SelectResponse selectResponse = pageBeanCRUDUtil.select(yunContext, selectRequest);
			TableData tableData = selectResponse.getTableDataMap().get(table.getTableName());
			pageBeanUtil.generateMListViewInfo(mListView, fieldList, tableData);
		}

		if (dataGrid != null) {
			query.setLimit(dataGrid.getPageSize());
			List<IDataBinding> dataGridFieldList = page.getAttribute(KEY_DATAGRID_FIELD_LIST);

			dataSourceUtil.fillQuery(yunContext, query, dataSourceConfig);

			SelectRequest selectRequest = new SelectRequest();
			selectRequest.setMainTable(table);
			selectRequest.setMainQuery(query);

			SelectResponse selectResponse = pageBeanCRUDUtil.select(yunContext, selectRequest);
			TableData tableData = selectResponse.getTableDataMap().get(table.getTableName());

			pageBeanUtil.generateDataGridInfo(dataGrid, dataGridFieldList, tableData);
		}
	}

	public void dataPage() {
		dataSearch();
	}

	protected Object getCurrentKeyValue() {
		YunContext yunContext = YunContext.getCurrentContext();
		Long idValue = Long.parseLong(yunContext.getViewRequest().getData().get("id"));
		return idValue;
	}

	public void dataEdit(String pageCode) {
		YunContext yunContext = YunContext.getCurrentContext();
		Long idValue = Long.parseLong(yunContext.getViewRequest().getData().get("id"));
		NavigatePageClientScript navigatePageClientScript = new NavigatePageClientScript();
		navigatePageClientScript.setPageId(yunContext.getPage().getId());
		navigatePageClientScript.setPageCode(pageCode);
		navigatePageClientScript.addPara("action", "dataEdit");
		navigatePageClientScript.addPara("id", idValue);
		yunContext.addClientScript(navigatePageClientScript);
		yunContext.setRenderCompleted(true);
	}

	public void dataNew(String pageCode) {
		YunContext yunContext = YunContext.getCurrentContext();
		NavigatePageClientScript navigatePageClientScript = new NavigatePageClientScript();
		navigatePageClientScript.setPageId(yunContext.getPage().getId());
		navigatePageClientScript.setPageCode(pageCode);
		navigatePageClientScript.addPara("action", "dataNew");
		yunContext.addClientScript(navigatePageClientScript);
		yunContext.setRenderCompleted(true);
	}

	public void dataFilter() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		DataSourceConfig dataSourceConfig = page.getDataSourceConfigList().get(0);
		Table table = tableStore.get(dataSourceConfig.getTableName());
		yunContext.setMasterModel(dataSourceConfig.getTableName());
		FilterGroup filterGroup = (FilterGroup) page.getAttribute(KEY_FILTERGROUP);
		String value = filterGroupComponentService.getSelectedValue(yunContext, filterGroup);
		Query query = new Query();
		query.setPage(1);
		if (!StringUtils.isEmpty(value)) {
			query.add(Restrictions.sql(value));
		}
		MListView mListView = (MListView) page.getAttribute(KEY_MLISTVIEW);
		DataGrid dataGrid = (DataGrid) page.getAttribute(KEY_DATAGRID);
		if (mListView != null) {
			query.setLimit(mListView.getPageSize());
			List<IDataBinding> fieldList = page.getAttribute(KEY_MLISTVIEW_FIELD_LIST);
			dataSourceUtil.fillQuery(yunContext, query, dataSourceConfig);

			SelectRequest selectRequest = new SelectRequest();
			selectRequest.setMainTable(table);
			selectRequest.setMainQuery(query);

			SelectResponse selectResponse = pageBeanCRUDUtil.select(yunContext, selectRequest);
			TableData tableData = selectResponse.getTableDataMap().get(table.getTableName());
			pageBeanUtil.generateMListViewInfo(mListView, fieldList, tableData);
		}

		if (dataGrid != null) {
			query.setLimit(dataGrid.getPageSize());
			List<IDataBinding> dataGridFieldList = page.getAttribute(KEY_DATAGRID_FIELD_LIST);
			dataSourceUtil.fillQuery(yunContext, query, dataSourceConfig);

			SelectRequest selectRequest = new SelectRequest();
			selectRequest.setMainTable(table);
			selectRequest.setMainQuery(query);

			SelectResponse selectResponse = pageBeanCRUDUtil.select(yunContext, selectRequest);
			TableData tableData = selectResponse.getTableDataMap().get(table.getTableName());

			pageBeanUtil.generateDataGridInfo(dataGrid, dataGridFieldList, tableData);
		}
	}
}
